using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Slimes
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GameState : Microsoft.Xna.Framework.GameComponent
    {
        public Int64 money { get; set; }

        public int white { get; set; }
        public int purple { get; set; }
        public int blue { get; set; }
        public int green { get; set; }
        public int yellow { get; set; }
        public int red { get; set; }
        private Game game;
        private Random rand;
        public Boolean gameover = false;

        List<Slime> Slimes = new List<Slime>();
        private int _slimeSelected = -2;
        public int slimeSelected
        {
            get
            {
                return _slimeSelected;
            }
            set
            {
                foreach (Slime slime in Slimes)
                {
                    slime.selected = false;
                }
                if (value > Slimes.Count - 1)
                {
                    _slimeSelected = 0;
                }
                else if (value == -1)
                {
                    _slimeSelected = Slimes.Count - 1;
                }
                else
                {
                    _slimeSelected = value;
                }
            }
        }

        public GameState(Game game)
            : base(game)
        {
            this.game = game;
            rand = new Random();

            white = 10000;
            purple = 5000;
            blue = 1000;
            green = 500;
            yellow = 100;
            red = 50;
            money = 100;

            Slimes.Add(Slime.newRed(game));
            Slimes.Add(Slime.newRed(game));

            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if (slimeSelected != -2)
                Slimes[slimeSelected].selected = true;

            if (gameTime.TotalGameTime.Milliseconds % 5000 == 0)
            {
                white++; purple++; blue++; green++; yellow++; red++;
            }

            // Random Mating every few seconds
            if (gameTime.TotalGameTime.Seconds % 20 == 0 &&
                gameTime.TotalGameTime.Milliseconds % 1000 == 0 &&
                gameTime.TotalGameTime.Seconds > 0 &&
                Slimes.Count > 1)
            {
                Slime mom = Slimes[rand.Next(Slimes.Count)];
                removeSlime(mom);
                Slime dad = Slimes[rand.Next(Slimes.Count)];
                removeSlime(dad);
                Slime.mate(game, mom, dad, 3, Slimes);
            }

            foreach (Slime slime in Slimes)
            {
                if (!game.Components.Contains(slime))
                {
                    game.Components.Add(slime);
                }
            }
                        
            base.Update(gameTime);
        }

        private void removeSlime(Slime toRemove)
        {
            slimeSelected = -2;
            Slimes.Remove(toRemove);
            game.Components.Remove(toRemove);
        }

        public bool buy(Slime.SlimeColor color)
        {
            switch (color)
            {
                case Slime.SlimeColor.WHITE:
                    if (!(money > white)) return false;
                    money -= white;
                    Slimes.Add(Slime.newWhite(game));
                    break;
                case Slime.SlimeColor.PURPLE:
                    if (!(money > purple)) return false;
                    money -= purple;
                    Slimes.Add(Slime.newPurple(game));
                    break;
                case Slime.SlimeColor.BLUE:
                    if (!(money > blue)) return false;
                    money -= blue;
                    Slimes.Add(Slime.newBlue(game));
                    break;
                case Slime.SlimeColor.GREEN:
                    if (!(money > green)) return false;
                    money -= green;
                    Slimes.Add(Slime.newGreen(game));
                    break;
                case Slime.SlimeColor.YELLOW:
                    if (!(money > yellow)) return false;
                    money -= yellow;
                    Slimes.Add(Slime.newYellow(game));
                    break;
                case Slime.SlimeColor.RED:
                    if (!(money > red)) return false;
                    money -= red;
                    Slimes.Add(Slime.newRed(game));
                    break;
            }
            return true;
        }

        public void sellSelected()
        {
            if (slimeSelected == -2) return;
            Slime.SlimeColor toSell = Slimes[slimeSelected].slimeColor;
            removeSlime(Slimes[slimeSelected]);
            switch (toSell)
            {
                case Slime.SlimeColor.RED:
                    money += red;
                    break;
                case Slime.SlimeColor.YELLOW:
                    money += yellow;
                    break;
                case Slime.SlimeColor.GREEN:
                    money += green;
                    break;
                case Slime.SlimeColor.BLUE:
                    money += blue;
                    break;
                case Slime.SlimeColor.PURPLE:
                    money += purple;
                    break;
                case Slime.SlimeColor.WHITE:
                    money += white;
                    break;
            }
        }
    }
}
